import {collision} from './collision';
import Bullet from '../../common/js/bullet'
/*
全局变量  getApp().data
myTankDir   我的坦克方向 0上 1 下 2 左 3 右
myTankPos  //我的坦克位置
MAP1         等级1地图
home         // 家的坐标
autoTankArray    //敌方坦克集合
drawMap      //画笔
autoTankNum   //敌方坦克数量
isHomeState //家的状态
mapSource //地图上所有的物体
*/
/**************坦克图片资源*****************/
var TANK_RESOURCE_IMAGE = new Image();
TANK_RESOURCE_IMAGE.src = 'common/images/tank.png';
/**************各个图块在坦克图片资源的位置*****************/
var POS = [];
POS['Tank'] = [0, 0]
var IMAGE_SPACE = 0;  //图片之间的间隔
var IMAGE_SIZE = 50   // 图片大小
/**************坦克及子弹的四个方向*****************/
var UP = 0;
var DOWN = 1;
var LEFT = 2;
var RIGHT = 3;
/**************子弹类型*****************/
var BULLET_TYPE_PLAYER = 5;    //玩家
var BULLET_TYPE_ENEMY = 4;     //敌方

export default class AutoTank {
  constructor(pos) {
    this.drawMap = getApp().data.drawMap;
    this.time = new Date();
    this.timer = null;
    this.pos = pos;
    this.dir = 1;// 子弹方向 0：上 1：下 2：左3：右
    this.isDestroyed = false; //是否被摧毁
    this.drawMap.drawImage(
      TANK_RESOURCE_IMAGE,
      POS['Tank'][0] + this.dir * IMAGE_SIZE + IMAGE_SPACE,
      POS['Tank'][1],
      IMAGE_SIZE,
      IMAGE_SIZE,
      this.pos[0] * IMAGE_SIZE,
      this.pos[1] * IMAGE_SIZE,
      IMAGE_SIZE,
      IMAGE_SIZE
    )
  }

  clear() {  //外部调用清除定时器
    clearInterval(this.timer);
  }

  auto() {
    this.timer = setInterval(() => {
      if (this.isDestroyed) {
        clearInterval(this.timer);
      }
      this.move();
      if (parseInt(Math.random() * 2)) {  //概率性发射子弹
        this.fire();
      }

    }, 4000)
  }

  fire() {
    let x = new Bullet(BULLET_TYPE_ENEMY, this.dir, this.pos);
    x.run();
  }

  move() {
    let oldPos = JSON.parse(JSON.stringify(this.pos));
    //console.info(oldPos);
    this.dir = parseInt(Math.random() * 4);
    switch (this.dir) {
      case UP: // 上
        this.pos[1] -= 1;
        break;
      case DOWN: //下
        this.pos[1] += 1;
        break;
      case LEFT: //左
        this.pos[0] -= 1;
        break;
      case RIGHT: //右
        this.pos[0] += 1;
        break;
    }
    switch (collision(this.pos)) {
      case 0: // 无障碍
        this.destroy(oldPos);
        this.create();
        console.log('敌方坦克在移动');
        break;
      case 1: // softWall
        this.pos = oldPos;
        this.create();
        console.log('敌方坦克撞可击毁墙');
        break;
      case 2: //  hardWall
        this.pos = oldPos;
        this.create();
        console.log('敌方的坦克撞不可击毁墙');
        break;
      case 3: //home
        this.pos = oldPos;
        this.create();
        console.log('敌方的坦克撞到家');
        break;
      case 4: // autoTank
        this.pos = oldPos;
        this.create();
        console.log('敌方坦克撞到敌方坦克');
        break;
      case 5: // myTank
        this.pos = oldPos;
        this.create();
        console.log('敌方坦克撞到我的坦克');
        break;
      case 6: //myBullet
        break;
      case 7: //bullet 由子弹决定击毁
        break;
      case 8: //border
        this.pos = oldPos;
        this.create();
        console.log('我的坦克撞边界了');
        break;
    }
  }

  // 新建
  create() {
    //撞到物品后，方向也会变化
    this.drawMap.drawImage(
      TANK_RESOURCE_IMAGE, POS['Tank'][0] + 50 * this.dir + IMAGE_SPACE,
      POS['Tank'][1], 50, 50, this.pos[0] * 50, this.pos[1] * 50, 50, 50
    );
    getApp().data.mapSource[this.pos[0]][this.pos[1]] = 4;  //移动
  }

  //销毁
  destroy(oldPos) {
    this.drawMap.clearRect(oldPos[0] * 50, oldPos[1] * 50, 50, 50);
    getApp().data.mapSource[oldPos[0]][oldPos[1]] = 0;  //移动
  }
}